package com.eurekaClient.utils;

import org.springframework.beans.factory.annotation.Value;
import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class RedisUtils {


    /*redis安装：https://www.runoob.com/redis/redis-install.html*/
    /*maven 地址：https://blog.csdn.net/weixin_45793065/article/details/106965693*/
    /*redis设置密码：https://blog.csdn.net/weixin_36239480/article/details/114070129*/

    @Value("${redis.server.ip}")
    private String redisServerIp;


    private static final Jedis redis = linkRedisUtils();
    private RedisUtils(){}

    private static Jedis linkRedisUtils(){

        String redisIp = "localhost";

        //连接本地的 Redis 服务
        Jedis jedis = new Jedis(redisIp);
        /* 密码 */
//        jedis.auth("123456");
        System.out.println("Redis 连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
        System.out.println("============================================================================================");
        return jedis;
    }

    public static Jedis linkRedis()
    {
        return redis;
    }


    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = linkRedis();

        //设置redis字符串数据
        jedis.set("pig", "3");
        //获取存储的数据并输出
        System.out.println("redis 存储的字符串为: "+ jedis.get("pig"));
        System.out.println("============================================================================================");

        //存储数据到列表中
        jedis.lpush("site-list", "Runoob");
        jedis.lpush("site-list", "Google");
        jedis.lpush("site-list", "Taobao");
        // 获取存储的数据并输出
        List<String> list = jedis.lrange("site-list", 0 ,2);
        for(String value : list) {
            System.out.println("列表项为: "+value);
        }
        System.out.println("============================================================================================");
        // 获取数据并输出
        Set<String> keys = jedis.keys("*");
        Iterator<String> it=keys.iterator() ;
        System.out.println("redis的所有key:");
        while(it.hasNext()){
            String key = it.next();
            System.out.println(key);
        }
    }
}
